/*
 * @Author: lmk
 * @Date: 2021-06-17 13:20:42
 * @LastEditTime: 2021-07-21 22:24:45
 * @LastEditors: lmk
 * @Description: common request
 */
import axios from 'axios'
import { store } from "@/stores";
export const baseURL = 'https://virtserver.swaggerhub.com/cwheart/sns/1.0.0/'
// create an axios instance
const request = axios.create({
  baseURL, // url = base url + request url http://47.100.235.21:12338
  // withCredentials: true, // send cookies when cross-domain requests
  timeout: 100000 // request timeout
})
// request interceptor
request.interceptors.request.use(
  config => {
    // do something before request is sent
    if(!config.headers['Content-Type']) config.headers['Content-Type'] = 'application/json;charset=UTF-8';
    const {loginForm} = store.getState().user
    if (loginForm.token) {
      config.headers['bearerAuth'] = loginForm.token
    }
    return config
  },
  error => {
    // do something with request error
    console.log(error) // for debug
    return Promise.reject(error)
  }
)

// response interceptor
request.interceptors.response.use(
  /**
   * If you want to get http information such as headers or status
   * Please return  response => response
  */

  /**
   * Determine the request status by custom code
   * Here is just an example
   * You can also judge the status by HTTP Status Code
   */
  response => {
    // const {data,message,success} = response.data;
    // if (!success) {
    //   console.log(message,'message')
    //   return Promise.reject(new Error(message || 'Error'))
    // } else {
    //   return data;
    // }
    return response.data
  },
  error => {
    console.log('err' + error.message) // for debug
    return Promise.reject(error)
  }
)

export default request